package com.yzxb.erpsystem.controller;

import com.yzxb.erpsystem.entity.Supplier;
import com.yzxb.erpsystem.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/suppliers")
public class SupplierController {
    
    @Autowired
    private SupplierService supplierService;
    
    /**
     * 创建供应商
     */
    @PostMapping
    public ResponseEntity<Supplier> createSupplier(@RequestBody Supplier supplier) {
        // 请求参数校验
        if (supplier == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Supplier createdSupplier = supplierService.createSupplier(supplier);
            return ResponseEntity.status(HttpStatus.CREATED).body(createdSupplier);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 更新供应商
     */
    @PutMapping("/{id}")
    public ResponseEntity<Supplier> updateSupplier(@PathVariable Long id, @RequestBody Supplier supplier) {
        // 请求参数校验
        if (id == null || supplier == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Supplier updatedSupplier = supplierService.updateSupplier(id, supplier);
            return ResponseEntity.ok(updatedSupplier);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 删除供应商
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            supplierService.deleteSupplier(id);
            return ResponseEntity.noContent().build();
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据ID查询供应商
     */
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplierById(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Optional<Supplier> supplier = supplierService.getSupplierById(id);
            return supplier.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据供应商编码查询供应商
     */
    @GetMapping("/code/{supplierCode}")
    public ResponseEntity<Supplier> getSupplierBySupplierCode(@PathVariable String supplierCode) {
        // 请求参数校验
        if (supplierCode == null || supplierCode.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Optional<Supplier> supplier = supplierService.getSupplierBySupplierCode(supplierCode);
            return supplier.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 查询所有供应商
     */
    @GetMapping
    public ResponseEntity<List<Supplier>> getAllSuppliers() {
        try {
            List<Supplier> suppliers = supplierService.getAllSuppliers();
            return ResponseEntity.ok(suppliers);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据状态查询供应商列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Supplier>> getSuppliersByStatus(@PathVariable String status) {
        // 请求参数校验
        if (status == null || status.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            List<Supplier> suppliers = supplierService.getSuppliersByStatus(status);
            return ResponseEntity.ok(suppliers);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 启用供应商
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Supplier> enableSupplier(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Supplier enabledSupplier = supplierService.enableSupplier(id);
            return ResponseEntity.ok(enabledSupplier);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 禁用供应商
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Supplier> disableSupplier(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Supplier disabledSupplier = supplierService.disableSupplier(id);
            return ResponseEntity.ok(disabledSupplier);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
}